package com.wisdom.client.cluster.lb;

import com.wisdom.client.cluster.ClientLoadBalanceStrategy;
import com.wisdom.core.annotation.WisdomLoadBalance;
import com.wisdom.core.enums.LoadBalanceStrategy;
import com.wisdom.core.provider.ServiceProvider;
import org.apache.commons.lang3.RandomUtils;

import java.util.List;

/**
 * @description: <随机负载均衡算法>
 * @author: Leo
 * @date: 2021-08-29 16:48
 */
@WisdomLoadBalance(strategy = LoadBalanceStrategy.RANDOM)
public class RanDomClientLoadBalance implements ClientLoadBalanceStrategy {
    @Override
    public ServiceProvider getProvider(List<? extends ServiceProvider> serviceProviders) {
        int index = RandomUtils.nextInt(0, serviceProviders.size());
        return serviceProviders.get(index);
    }
}
